package com.example.zhu.service.Impl;

import com.example.zhu.config.BaseContext;
import com.example.zhu.entity.Transactions;
import com.example.zhu.entity.UserMoney;
import com.example.zhu.entity.Withdraw;
import com.example.zhu.mapper.MoneyMapper;
import com.example.zhu.mapper.TransactionMapper;
import com.example.zhu.mapper.UsersMapper;
import com.example.zhu.result.Result;
import com.example.zhu.service.MoneyService;
import com.example.zhu.vo.WithDrawVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MoneyServiceImpl implements MoneyService {


    @Autowired
    private MoneyMapper moneyMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private TransactionMapper transactionMapper;

    /**
     * 查询所有提现申请
     * @return
     */
    public List<WithDrawVO> getWithdraws() {
        //查询提现申请记录
        List<WithDrawVO> list = moneyMapper.getWithdraws();
        return list;
    }

    /**
     * 更新操作
     * @param withdrawId
     * @return
     */
    @Transactional
    public Result<Object> update(Integer withdrawId) {

        //获取提现用户的openid
        Withdraw withdraw = moneyMapper.selectById(withdrawId);
        String openid = withdraw.getOpenid();

        //获取提现金额
        Integer withdrawMoney = withdraw.getWithdrawMoney();

        //获取提现前的金额
        UserMoney userMoneyBefore = usersMapper.selectMoney(openid);
        Integer before = userMoneyBefore.getMoney();

        //计算提现后的金额
        Integer after = before - withdrawMoney;

        try {
            //1.更新提现状态
            moneyMapper.updateState(withdrawId);

            //2.修改用户余额
            if(after < 0){
                return Result.error("余额不足");
            }
            moneyMapper.updateMoney(openid,withdrawMoney);

        } finally {

            //3.生成交易记录
            log(openid, userMoneyBefore, before, after);
        }

        return Result.success();
    }

    /**
     * 开启新事务，能够保证日志的生成
     * @param openid
     * @param userMoneyBefore
     * @param before
     * @param after
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void log(String openid, UserMoney userMoneyBefore, Integer before, Integer after) {
        Transactions transactions = Transactions.builder().openid(BaseContext.getCurrentUsername())
                .takeOpenid(openid)
                .number(System.currentTimeMillis())
                .coinsBefore(userMoneyBefore.getPaopaoCoins())
                .coinsAfter(userMoneyBefore.getPaopaoCoins())
                .moneyBefore(before)
                .moneyAfter(after)
                .transactionDate(LocalDateTime.now())
                .transactionType("提现")
                .fee(0)
                .build();
        transactionMapper.insert(transactions);
    }
    /**
     * 根据id查询提现记录
     * @param id
     * @return
     */
    public WithDrawVO selectById(Integer id) {
       WithDrawVO withDrawVO = moneyMapper.getWithdraw(id);
       return withDrawVO;
    }

    /**
     * 根据手机号查询提现记录
     * @return
     */
    public List<WithDrawVO> getByPhone(String phone) {
        List<WithDrawVO> list = moneyMapper.getByPhone(phone);
        return list;
    }

    /**
     * 删除提现记录
     * @param id
     * @return
     */
    public Result deleteById(Integer id) {

        //1.查询当前记录的状态是否已审核
        Withdraw withdraw = moneyMapper.selectById(id);
        if(withdraw.getStatus()==0){
            return Result.error("该订单未审核，无法删除");
        }

        //2.可以删除
        moneyMapper.deleteById(id);
        return Result.success("删除成功");
    }


}
